package com.ruoyi.web.controller.jimu.admin;


import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.jimu.service.IJmReportService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jimu.domain.JmReportType;
import com.ruoyi.jimu.service.IJmReportTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

@RestController("AdminJmReportTypeController")
@RequestMapping("/admin/jimu/report/type")
public class JmReportTypeController extends BaseController {

    @Resource
    private IJmReportTypeService jmReportTypeService;


    @Resource
    private IJmReportService jmReportService;

    /**
     * 查询报表类型管理列表
     */
    //@PreAuthorize("@ss.hasPermi('jimu:type:list')")
    @GetMapping("/page/list")
    public TableDataInfo pageList(JmReportType jmReportType) {
        startPage();
        List<JmReportType> list = jmReportTypeService.selectJmReportTypeList(jmReportType);
        return getDataTable(list);
    }


    /**
     * 查询报表类型管理列表
     *
     * @param jimuReportType
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(JmReportType jimuReportType) {
        return success(jmReportTypeService.selectJmReportTypeList(jimuReportType));
    }

    /**
     * 导出报表类型管理列表
     */
    //@PreAuthorize("@ss.hasPermi('jimu:type:export')")
    @Log(title = "报表类型管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JmReportType jmReportType) {
        List<JmReportType> list = jmReportTypeService.selectJmReportTypeList(jmReportType);
        ExcelUtil<JmReportType> util = new ExcelUtil<JmReportType>(JmReportType.class);
        util.exportExcel(response, list, "报表类型管理数据");
    }

    /**
     * 获取报表类型管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('jimu:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jmReportTypeService.selectJmReportTypeById(id));
    }

    /**
     * 新增报表类型管理
     */
    //@PreAuthorize("@ss.hasPermi('jimu:type:add')")
    @Log(title = "报表类型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JmReportType jmReportType) {
        return toAjax(jmReportTypeService.insertJmReportType(jmReportType));
    }

    /**
     * 修改报表类型管理
     */
    //@PreAuthorize("@ss.hasPermi('jimu:type:edit')")
    @Log(title = "报表类型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JmReportType jmReportType) {
        return toAjax(jmReportTypeService.updateJmReportType(jmReportType));
    }

    /**
     * 删除报表类型管理
     */
    @Log(title = "报表类型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        //判断当前分类下，是否有子分类，如果有子分类，无法删除当前分类
        JmReportType jmReportType = jmReportTypeService.selectJmReportTypeById(id);
        if (ObjectUtil.isEmpty(jmReportType)) {
            return success("未找到当前报表分类，可能已被删除");
        }
        if (jmReportTypeService.selectChildrenCount(id) > 0L) {
            return error("当前报表分类下还存在子分类，暂时无法删除！");
        }
        if (jmReportTypeService.selectContainReportCount(id) > 0L) {
            return error("当前报表分类下还存在报表，暂时无法删除！");
        }
        return toAjax(jmReportTypeService.deleteJmReportTypeById(id));
    }
}
